package com.srwl.mytx.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.florent37.viewanimator.AnimationListener;
import com.github.florent37.viewanimator.ViewAnimator;
import com.srwl.mytx.utils.DateUtils;
import com.srwl.mytx.R;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CountDownView extends LinearLayout {

    //    @BindView(R.id.tv_start_time)
//    TextView tv_bidIncrement;
    @BindView(R.id.tv_count_down)
    TextView tv_countDown;

    @BindView(R.id.tv_second)
    TextView tv_second;
    @BindView(R.id.tv_second_end)
    TextView tv_secondEnd;
    private Context context;
    private String countDown;
    private long days;
    private long hours;
    private long minutes;
    private long second;
    private Date startDate;
    private Date stopDate;
    private String secondEndTips;
    private String countDownStopTips;//倒计时结束后显示的提示语
    private boolean isStart;
    private InteractionListener listener;
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {

            ViewAnimator.animate(tv_second).translationY(0, -tv_second.getHeight()).duration(300).onStop(new AnimationListener.Stop() {
                @Override
                public void onStop() {

                    if (isAuctionEnd) {//当倒计时结束的时候，隐藏掉显示秒相关的两个TextView
                        tv_countDown.setText(countDownStopTips);
                        tv_second.setVisibility(View.GONE);
                        tv_secondEnd.setVisibility(View.GONE);
                    } else {
                        tv_countDown.setText(countDown);
                        tv_second.setText(second + "");
                        ViewAnimator.animate(tv_second).translationY(tv_second.getHeight(), 0).duration(300).start();
                    }
                }
            }).start();
            if (!isAuctionEnd) {//倒计时没有结束，继续递归执行
                handler.postDelayed(runnable, 1000);
                isStart=true;
            } else {//倒计时结束，隐藏掉输入框和提交按键
                if (listener != null) {
                    listener.onCountDownStop();
                    isStart=false;
                }

            }

            //开始计算下一次显示的时间
            second--;
            if (second < 0) {//秒 小于0 ，就考虑分 是否还大于0，
                if (minutes > 0) {//如果分钟数大于0，分钟数减去 1，则把秒设置为59秒，不涉及小时和天
                    minutes--;
                    second = 59;
                    countDown = generateCountDown();
                    return;
                }
            } else {
                countDown = generateCountDown();
                return;
            }
            //分钟和秒都出现0，判断小时数量是否大于0，如果小时数量大于0，小时数量减去1，秒设置为59，分钟数设为59，不涉及天
            if (hours > 0) {
                hours--;
                second = 59;
                minutes = 59;
                countDown = generateCountDown();
                return;
            }

            //小时 分钟  秒  都出现0，判断天数是否大于0
            if (days > 0) {
                days--;
                second = 59;
                minutes = 59;
                hours = 23;
                countDown = generateCountDown();
                return;
            }
            isAuctionEnd = true;
        }
    };
    private boolean isAuctionEnd;

    public CountDownView(Context context) {
        super(context);
        this.context = context;
        init(null);
    }

    public CountDownView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(attrs);
    }

    public CountDownView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(attrs);
    }


    private void init(AttributeSet attrs) {
        inflate(context, R.layout.count_down_layout, this);
        ButterKnife.bind(this);
        if (attrs == null) {
            return;
        }
        TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.CountDownView);//获取配置属性
        int secondTextColor = tArray.getInt(R.styleable.CountDownView_secondTextColor, getResources().getColor(R.color.rp_money_red_light));
        int otherTextColor = tArray.getInt(R.styleable.CountDownView_otherTextColor, getResources().getColor(R.color.text_black));
        float secondTextSize = tArray.getFloat(R.styleable.CountDownView_secondTextSize, 12f);
        float otherTextSize = tArray.getFloat(R.styleable.CountDownView_otherTextSize, 12f);
        countDownStopTips = tArray.getString(R.styleable.CountDownView_countDownStopTips);
        if (TextUtils.isEmpty(countDownStopTips)) {
            countDownStopTips = "竞价已经结束";
        }
        secondEndTips = tArray.getString(R.styleable.CountDownView_secondEndTips);
        if (TextUtils.isEmpty(secondEndTips)) {
            secondEndTips = "秒竞价结束";
        }
        tv_second.setTextColor(secondTextColor);
        tv_countDown.setTextColor(otherTextColor);
        tv_secondEnd.setTextColor(otherTextColor);
        tv_second.setTextSize(secondTextSize);
        tv_countDown.setTextSize(otherTextSize);
        tv_secondEnd.setTextSize(otherTextSize);
        tv_secondEnd.setText(secondEndTips);
        tArray.recycle();
    }

    public void init(String secondEndTips,Date stopDate,   InteractionListener listener) {
        this.secondEndTips=secondEndTips;
        tv_secondEnd.setText(secondEndTips);
        this.listener = listener;
        long stopDateTime = stopDate.getTime();
        long currentDateTime = System.currentTimeMillis();
        long diff = stopDateTime - currentDateTime;
        computeAuctionEndDate(diff);
        countDown = generateCountDown();
        handler.post(runnable);
        isStart=true;

    }

    public void init(Date startDate, Date stopDate, InteractionListener listener) {
        this.listener = listener;
        long startDateTime = startDate.getTime();
        long stopDateTime = stopDate.getTime();
        long currentDateTime = System.currentTimeMillis();
        if (startDateTime > currentDateTime) {
            tv_countDown.setText(DateUtils.getTimestampString(startDate) + "竞价开始");
        } else if (stopDateTime > currentDateTime) {

            long diff = stopDateTime - currentDateTime;
            computeAuctionEndDate(diff);
            countDown = generateCountDown();
            handler.post(runnable);
            isStart=true;
        } else {
            isAuctionEnd = true;
            tv_countDown.setText(countDownStopTips);
            tv_secondEnd.setVisibility(View.GONE);
            if (listener != null) {
                listener.onCountDownStop();
            }
        }
    }

    private String generateCountDown() {

        // return "还有" + days + "天" + hours + "小时" + minutes + "分" + second + "秒竞价结束";
        if (days != 0) {
            return "还有" + days + "天" + hours + "小时" + minutes + "分";
        } else if (hours != 0) {
            return "还有" + hours + "小时" + minutes + "分";
        } else {
            return "还有" + minutes + "分";
        }
    }

    private void computeAuctionEndDate(long diff) {
        days = diff / (1000 * 60 * 60 * 24);
        hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
        second = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;
    }

    public boolean isStart() {
        return isStart;
    }

    public void clear(){
        handler.removeCallbacks(runnable);
    }

    public interface InteractionListener {
        void onCountDownStop();
    }
}
